package com.lgCore.cocurrent;/**
 * Created by by on 2020/11/13 10:35
 */

import com.lgCore.netty.message.ReplyServerBody;
import com.lgCore.netty.server.handler.NettyServerHandler;
import com.lgCore.server.factory.CommonRpcServiceFactory;
import com.lgCore.server.workHandler.invokBean.InvokRpcMethod;
import com.lgCore.util.ExceptionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.Callable;

/**
 * invok异步执行
 *
 * @program: sxreader_rpc
 * @author: ligang
 * @create: 2020-11-13 10:35
 */
public class ServiceInvokCallable implements Callable<ReplyServerBody> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInvokCallable.class);

    private InvokRpcMethod invokRpcMethod;
    public ServiceInvokCallable(InvokRpcMethod invokRpcMethod) {
        this.invokRpcMethod = invokRpcMethod;
    }

    @Override
    public ReplyServerBody call() throws Exception {
        ReplyServerBody replyBody = null;
        try {
            Object obj = null;
            Date date = new Date();
            obj = CommonRpcServiceFactory.getCommonRpcRouteService().methodInvoke(invokRpcMethod);
//            LOGGER.info("接口响应时间："+(new Date().getTime()-date.getTime()));
            replyBody = new ReplyServerBody(obj, 1);
        } catch (Exception e) {
            String errorMsg = ExceptionUtil.ExceptionDetailAll(e);
            replyBody = new ReplyServerBody(null, 0);
            replyBody.setExceptionMsg(errorMsg);
            LOGGER.info("接口调用失败：" + errorMsg);
            e.printStackTrace();
        }
        return replyBody;
    }
}
